#!/usr/bin/env python3
"""
修复菜单路径脚本
将后端菜单路径修改为与前端路由匹配的路径
"""

from app import create_app, db
from app.models.menu import Menu

def fix_menu_paths():
    """修复菜单路径，使其与前端路由匹配"""
    app = create_app()
    
    with app.app_context():
        # 路径映射表：后端路径 -> 前端路径
        path_mappings = {
            '/system/admins': '/adminManage',
            '/system/departments': '/departmentManage', 
            '/system/menus': '/menuManage',
            '/system/roles': '/roleManage',
            '/users': '/userManage',
            '/analysis': '/analysis',  # 这个路径保持不变，稍后需要在前端路由中添加
            '/system': ''  # 父菜单通常不需要路径，或者设为空
        }
        
        print("开始修复菜单路径...")
        print("=" * 50)
        
        # 获取所有菜单
        menus = Menu.query.all()
        
        for menu in menus:
            old_path = menu.path
            
            if old_path in path_mappings:
                new_path = path_mappings[old_path]
                menu.path = new_path
                
                print(f"修复菜单: {menu.name}")
                print(f"  旧路径: {old_path}")
                print(f"  新路径: {new_path}")
                print("-" * 30)
            else:
                print(f"跳过菜单: {menu.name} (路径: {old_path}) - 无需修改")
        
        # 提交修改
        db.session.commit()
        print("\n菜单路径修复完成！")
        
        # 验证修复结果
        print("\n修复后的菜单列表:")
        print("=" * 50)
        menus = Menu.query.all()
        for menu in menus:
            parent_info = f" (父菜单: {menu.parent.name})" if menu.parent else ""
            print(f"ID:{menu.id} - {menu.name} - {menu.path}{parent_info}")

if __name__ == "__main__":
    fix_menu_paths() 